#include "StdAfx.h"
#include "DB.h"
#include "cppmysql.h"

CDB sDB;
CDB::CDB(void)
{
	m_pDB = new CppMySQL3DB;
	m_pBuff = new char[1024*1000];
}

CDB::~CDB(void)
{
	delete m_pDB;
}

int CDB::ConnectDB(const char* szIp,const char* szDBName)
{
	int ret = m_pDB->open(szIp, "root", "111111", szDBName);
	if ( ret != 0 )
	{
		IMSG(_T("db.open false"));
		return -1;
	}
	else
		IMSG(_T("db.open OK"));
	return ret;
}

int CDB::ExecSQL(const char* sql)
{
	int iRst = m_pDB->execSQL(sql);
	return iRst;
}

int CDB::QuerySQL(const char* sql,DWORD dwSize, FORMATFUN func,DWORD& nNums, void* lpData)
{
	CppMySQLQuery q = m_pDB->querySQL(sql);
	nNums = q.numRow();
	while(!q.eof())
	{
		(this->*func)(&q, lpData);
		lpData = (char*)lpData+dwSize;
		q.nextRow();
	}
	return 0;
}

void CDB::FormatUserInformation(void* q, void* lpData)
{
	CppMySQLQuery* pQ = (CppMySQLQuery*)q;

	//tagNC_RoleLogin* pRoleInfor = (tagNC_RoleLogin*) lpData;
	//strncpy_s(pRoleInfor->szName,32,pQ->getStringField("name"), 32);
	//strncpy_s(pRoleInfor->szPassword,32, pQ->getStringField("password"), 32);
}

DWORD CDB::LoadUserInformation(void* lpData)
{
	string strSQL = "select *  from userinfor";
	//QuerySQL(strSQL.c_str(), &CDB::FormatUserInformation, lpData);
	return 0;
}

DWORD CDB::PlayerAuthSession(void* lpData)
{
	//tagNDBC_PlayerAuthSession *pMsg = (tagNDBC_PlayerAuthSession*)lpData;
	//string strSQL;
	//strSQL = "select * from userinfor where name='";
	//strSQL +=pMsg->szName;
	//strSQL +="'";
	//CppMySQLQuery q = m_pDB->querySQL(strSQL.c_str());
	//if(q.numRow()<0)
	//	return EAS_Name_No_Exist;
	//if(0 != strcmp(pMsg->szPassword, q.getStringField("password")))
	//	return EAS_Password_Error;
	return EAS_Sucessed;
}

void CDB::FormatRoleInfor(void* q, void* lpData)
{
	CppMySQLQuery* pQ = (CppMySQLQuery*)q;

	//tagRoleInfor* pRoleInfor = (tagRoleInfor*) lpData;
	//strncpy_s(pRoleInfor->szName, 32, pQ->getStringField("name"), 32);
	//pRoleInfor->dwID = pQ->getIntField("id");
}

DWORD CDB::LoadRoleInfor(void* &lpData, DWORD& dwSize)
{
	//string strSQL = "select *  from userinfor";
	//tagNDBS_LoadRoleInfor* pMsg = (tagNDBS_LoadRoleInfor*)m_pBuff;
	//QuerySQL(strSQL.c_str(), sizeof(tagRoleInfor), &CDB::FormatRoleInfor, pMsg->dwNums, pMsg->pRoleInfor);
	//pMsg->dwSize = sizeof(tagRoleInfor)*pMsg->dwNums + sizeof(tagNDBS_LoadRoleInfor)-1;
	//lpData = m_pBuff;
	//dwSize = pMsg->dwSize;
	return 0;
}